
#ifndef __KHAN_VTY_MANAGER_HPP__
#define __KHAN_VTY_MANAGER_HPP__

#include "khan.hpp"
#include "io_session.hpp"
#include "vty_session.hpp"
#include "vty_dft_command.hpp"

class vty_manager;
typedef boost::shared_ptr<vty_manager> vty_manager_ptr;

class vty_manager
: private boost::noncopyable
{
public:
    static vty_manager_ptr instance();
	~vty_manager()
	{ _alarm_ostream.close(); }

    void join(vty_session_ptr s);
    void leave(vty_session_ptr s);
    void stop_all();
    void receive_alram(const std::string& msg);

	bool get_alarm_flag() { return _alarm_flag; }
	void ctrl_alarm(bool onoff);
	const std::string& alarm_file_name();

    io_session_ptr create_session(io_service& ios);
    vty_ptr find_vty(__u32 vty_id);

private:
	typedef std::map<__u32, vty_session_ptr> session_map_t;
	typedef session_map_t::iterator	session_map_iterator;

    vty_manager():
        _vty_num(0),
		_alarm_flag(true),
		_alarm_fname("/tmp/alarm.txt"),
		_alarm_ostream("/tmp/alarm.txt")
    { };



    static vty_manager_ptr _instance;
    static boost::mutex _singletone_lock;

    int _vty_num;   /* FIXME: change algo. */
    //std::set<vty_session_ptr> _sessions;
    session_map_t  	_sessions;
	bool			_alarm_flag;
	std::string 	_alarm_fname;
	std::ofstream	_alarm_ostream;
	boost::mutex	_alarm_lock;
	boost::mutex	_session_lock;
};



#endif /* __KHAN_VTY_MANAGER_HPP__ */
